#include "VDockWidget.h"
#include "ui_VDockWidget.h"
#include "VTitleBarWidget.h"

#include <QDebug>

VDockWidget::VDockWidget(const QString &title,  int close_type) :
    QDockWidget(title, Q_NULLPTR, Qt::WindowFlags()),
    _close_type(close_type),
    ui(new Ui::VDockWidget)
{
    ui->setupUi(this);

    VTitleBarWidget *w = new VTitleBarWidget(this);
    _toolbar = w->get_toolbar();
    this->setTitleBarWidget(w);
    connect(w, &VTitleBarWidget::signal_dock_setting, this, &VDockWidget::slot_dock_setting);
    connect(w, &VTitleBarWidget::signal_close_clicked, this, &VDockWidget::slot_close_clicked);
    this->setWindowTitle(title);
}

VDockWidget::~VDockWidget()
{
    delete ui;
}

void VDockWidget::slot_dock_lock(int width, int height)
{
    qDebug() << width << " " << this->width();
    qDebug() << height << " " << this->height();

    if(_w && width != this->width())
    {
        setMaximumWidth(this->width());
        setMinimumWidth(this->width());
    }

    if(_h && height != this->height())
    {
        setMaximumHeight(this->height());
        setMinimumHeight(this->height());
    }
}

void VDockWidget::slot_dock_unlock()
{
    qDebug() << "unlock";
    setMaximumWidth(16777215);
    setMinimumWidth(0);
    setMaximumHeight(16777215);
    setMinimumHeight(0);
}

void VDockWidget::slot_dock_setting(bool w, bool h)
{
    _w = w;
    _h = h;
}

void VDockWidget::slot_close_clicked()
{
    if(_close_type == CloseType::HIDE)
    {
        this->hide();
    }
    else if(_close_type == CloseType::EMIT)
    {
        emit signal_close_clicked();
    }
}

void VDockWidget::slot_set_title(QString title)
{
    this->setWindowTitle(title);
}
